package cn.gov.pbc.tims.controller;

import java.io.IOException;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import cn.gov.pbc.tims.entity.Dept;
import cn.gov.pbc.tims.entity.Jginfo;
import cn.gov.pbc.tims.entity.User;
import cn.gov.pbc.tims.service.CommonService;
import cn.gov.pbc.tims.service.UserService;
import cn.gov.pbc.tims.util.Operator;
import cn.gov.pbc.tims.util.PageData;
import cn.gov.pbc.tims.util.Result;

@Controller
@RequestMapping("/Dept")
public class DeptController {
	@Autowired
	private Operator operator;
	@Autowired
	private SessionFactory sessionFactory;
	@Autowired
	private CommonService commonService;
	@Autowired 
	private ObjectMapper objectMapper;
	private static ArrayNode jgCache;
	private Session getCurrentSession() {
		return this.sessionFactory.getCurrentSession();
	}
	
 
 
	
	private List<Dept> getChildrenDept(Integer deptId){
 		Query<Dept> q = this.getCurrentSession().createQuery("from Dept where  upDeptId='"+deptId+"'", Dept.class).setCacheable(true);
		List<Dept> Depts = q.getResultList();
		return Depts;
 	}
 	  
	@RequestMapping(value="/getJgDeptList")
	public @ResponseBody ArrayNode getDeptList(String  jgId) throws JsonProcessingException, IOException{
		ArrayNode jsonArray=objectMapper.createArrayNode();
		User op=operator.getUser();
		Query<Dept> q = this.getCurrentSession().createQuery("from Dept where level='1'   and jgId= '" + jgId + "'", Dept.class).setCacheable(true);
		List<Dept> DeptLevel1 = q.getResultList();
		for (Dept dept:DeptLevel1)
		{
			ObjectNode jsonObject=objectMapper.createObjectNode();
			jsonObject.set("id",objectMapper.convertValue(dept.getId(),JsonNode.class));
			jsonObject.set("text",objectMapper.convertValue(dept.getDeptName(),JsonNode.class));
			jsonObject.set("state",objectMapper.convertValue("open",JsonNode.class));
			ArrayNode jsonArray1=objectMapper.createArrayNode();
			for(Dept deptlevel2 : getChildrenDept(dept.getId())){
				ObjectNode jo=objectMapper.createObjectNode();
				jo.set("id",objectMapper.convertValue(deptlevel2.getId(),JsonNode.class));
				jo.set("text",objectMapper.convertValue(deptlevel2.getDeptName(),JsonNode.class));
				jo.set("state",objectMapper.convertValue("open",JsonNode.class));				
				jsonArray1.add(jo);
			}
			if(jsonArray1.size()>0){	
				jsonObject.set("children", jsonArray1);
			}
			jsonArray.add(jsonObject);
		}
 
		return jsonArray;		
	}
	@RequestMapping(value="/getDeptList")
	public @ResponseBody ArrayNode getDeptList() throws JsonProcessingException, IOException{
		ArrayNode jsonArray=objectMapper.createArrayNode();
		User op=operator.getUser();
		Query<Dept> q = this.getCurrentSession().createQuery("from Dept where level='1'", Dept.class).setCacheable(true);
		List<Dept> DeptLevel1 = q.getResultList();
		for (Dept dept:DeptLevel1)
		{
			ObjectNode jsonObject=objectMapper.createObjectNode();
			jsonObject.set("id",objectMapper.convertValue(dept.getId(),JsonNode.class));
			jsonObject.set("text",objectMapper.convertValue(dept.getDeptName(),JsonNode.class));
			jsonObject.set("state",objectMapper.convertValue("open",JsonNode.class));
			ArrayNode jsonArray1=objectMapper.createArrayNode();
			for(Dept deptlevel2 : getChildrenDept(dept.getId())){
				ObjectNode jo=objectMapper.createObjectNode();
				jo.set("id",objectMapper.convertValue(deptlevel2.getId(),JsonNode.class));
				jo.set("text",objectMapper.convertValue(deptlevel2.getDeptName(),JsonNode.class));
				jo.set("state",objectMapper.convertValue("open",JsonNode.class));				
				jsonArray1.add(jo);
			}
			if(jsonArray1.size()>0){	
				jsonObject.set("children", jsonArray1);
			}
			jsonArray.add(jsonObject);
		}
 
		return jsonArray;		
	}
	@RequestMapping(value="/getDeptTree")
	public @ResponseBody ArrayNode getDeptTree() throws JsonProcessingException, IOException{
		ArrayNode jsonArray=objectMapper.createArrayNode();
		User op=operator.getUser();
		Query<Dept> q = this.getCurrentSession().createQuery("from Dept where level='1'", Dept.class).setCacheable(true);
		List<Dept> DeptLevel1 = q.getResultList();
		for (Dept dept:DeptLevel1)
		{
			ObjectNode jsonObject=objectMapper.createObjectNode();
			jsonObject.set("id",objectMapper.convertValue(dept.getId(),JsonNode.class));
			jsonObject.set("deptName",objectMapper.convertValue(dept.getDeptName(),JsonNode.class));
			jsonObject.set("level",objectMapper.convertValue(dept.getLevel(),JsonNode.class));
			jsonObject.set("jgId",objectMapper.convertValue(dept.getJgId(),JsonNode.class));
			jsonObject.set("state",objectMapper.convertValue("open",JsonNode.class));
			ArrayNode jsonArray1=objectMapper.createArrayNode();
			for(Dept deptlevel2 : getChildrenDept(dept.getId())){
				ObjectNode jo=objectMapper.createObjectNode();
				jo.set("id",objectMapper.convertValue(deptlevel2.getId(),JsonNode.class));
				jo.set("deptName",objectMapper.convertValue(deptlevel2.getDeptName(),JsonNode.class));
				jo.set("level",objectMapper.convertValue(deptlevel2.getLevel(),JsonNode.class));
				jo.set("jgId",objectMapper.convertValue(deptlevel2.getJgId(),JsonNode.class));
				jo.set("state",objectMapper.convertValue("open",JsonNode.class));			
				jsonArray1.add(jo);
			}
			if(jsonArray1.size()>0){	
				jsonObject.set("children", jsonArray1);
			}
			jsonArray.add(jsonObject);
		}
 
		return jsonArray;		
	}
	@RequestMapping(value="/getDeptUsers" )
	public @ResponseBody List<User> getDeptUsers(String deptId) throws JsonProcessingException, IOException{
		 
 		String hql="from User where deptId=:deptId";
		Query  query = this.getCurrentSession().createQuery(hql, User.class).setCacheable(true);
		query.setInteger("deptId", Integer.valueOf(deptId));
		List<User> users = query.getResultList();
		return users;
	}	
		
	@RequestMapping("/get")
	public @ResponseBody Dept get(String id) {
		Result result = new Result();
		/*if (exist(dept)) {
			result.setValue(1, "该用户代码已经被使用！");
			return result;
		}*/
		Dept dept=commonService.get(Dept.class,Integer.valueOf(id));
		Dept upDept=null;
		if(null!=dept.getUpDeptId())
		{
			upDept=commonService.get(Dept.class,dept.getUpDeptId());
			result.setData(upDept);
		}
		return upDept;
	} 
	@RequestMapping("/save")
	public @ResponseBody Result save(Dept dept) {
		Result result = new Result();
		/*if (exist(dept)) {
			result.setValue(1, "该用户代码已经被使用！");
			return result;
		}*/
		dept.setJgId(operator.getUser().getJgId());
		if(null==dept.getUpDeptId())
			dept.setLevel("1");	
		else
			{
			Dept upDept=commonService.get(Dept.class,dept.getUpDeptId());
			dept.setLevel(String.valueOf((Integer.valueOf(upDept.getLevel())+1)));	
			}
		commonService.saveOrUpdate(dept);
		result.setData(dept);
		result.setValue(0, "用户保存成功！");
		return result;
	}
	
	@RequestMapping("/edit")
	public @ResponseBody Result edit(String id,Dept dept) {
		Result result = new Result();
		if(null==dept.getUpDeptId())
			dept.setLevel("1");	
		else
			{
			Dept upDept=commonService.get(Dept.class,dept.getUpDeptId());
			dept.setLevel(String.valueOf((Integer.valueOf(upDept.getLevel())+1)));	
			}
		
		commonService.updateEntitie(dept);
		result.setData(dept);
		result.setValue(0, "部门信息保存成功！");
		return result;
	}
	 
	 
	 
}
